#coding=utf-8
import re
import requests,json,urllib
import urllib
import os


def getphotos():
    s = requests.session()
    print u'开始爬取头像和用户名'
    for page in range(1,625,1):
        index = s.get('https://tieba.baidu.com/p/4172141785?pn='+str(page))
        #通过正则取出所有的src连接和username
        photo = re.findall(r'username="(.+)" class="" src="(.+)"', index.text)
        try:
            for i in range(0,27,1):
                username = photo[i][0]
                src = photo[i][1]
                src = str(src).replace('//tb2.bdstatic.com/tb/static-pb/img/head_80.jpg" data-tb-lazyload="','')
                new_src = src.replace('https','http')
                print username,new_src
                urllib.urlretrieve(new_src, 'D:\\images\\%s.jpg' % username)

        except:
            print u'异常！'


        #下载的主函数
if __name__ =='__main__':
    getphotos()